﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using XiaFControl.Demo.Views;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace XiaFControl.Demo
{
    public partial class MainViewModel : ObservableObject
    {
        Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();

        const string PROJECT_NAME = "XiaFControl.Demo";
        public TextEditor TextEditor;

        [ObservableProperty]
        private ObservableCollection<ControlItem> controlList;

        [ObservableProperty]
        private ObservableCollection<ControlItem> customerControlList;

        [ObservableProperty]
        private ObservableCollection<ControlItem> businessControlList;

        [ObservableProperty]
        private object content;

        public MainViewModel()
        {
            ControlList = new ObservableCollection<ControlItem>();
            ControlList.Add(new ControlItem() { Name = "按钮", NaviName = nameof(ButtonDemo) });
            ControlList.Add(new ControlItem() { Name = "文本框", NaviName = nameof(TextBoxDemo) });
            ControlList.Add(new ControlItem() { Name = "下拉框", NaviName = nameof(ComboBoxDemo) });
            ControlList.Add(new ControlItem() { Name = "数值选择", NaviName = nameof(NumericUpDownDemo) });
            ControlList.Add(new ControlItem() { Name = "单选按钮", NaviName = nameof(RadioButtonDemo) });
            ControlList.Add(new ControlItem() { Name = "切换按钮", NaviName = nameof(ToggleButtonDemo) });
            ControlList.Add(new ControlItem() { Name = "复选框", NaviName = nameof(CheckBoxDemo) });
            ControlList.Add(new ControlItem() { Name = "加载中", NaviName = nameof(LoadingDemo) });
            ControlList.Add(new ControlItem() { Name = "滑块", NaviName = nameof(SliderDemo) });
            //ControlList.Add(new ControlItem() { Name = "密码框", NaviName = nameof(PasswordBoxDemo) });
            ControlList.Add(new ControlItem() { Name = "选项卡", NaviName = nameof(TabControlDemo) });
            ControlList.Add(new ControlItem() { Name = "表格", NaviName = nameof(DataGirdDemo) });
            //ControlList.Add(new ControlItem() { Name = "选项卡", NaviName = nameof(XiaFTabControlDemo) });
            //ControlList.Add(new ControlItem() { Name = "按钮", NaviName = nameof(XiaFButtonDemo) });

            CustomerControlList = new ObservableCollection<ControlItem>();
            CustomerControlList.Add(new ControlItem() { Name = "按钮", NaviName = nameof(XiaFButtonDemo) });
            CustomerControlList.Add(new ControlItem() { Name = "选项卡", NaviName = nameof(XiaFTabControlDemo) });
            CustomerControlList.Add(new ControlItem() { Name = "文本框", NaviName = nameof(XiaFTextBoxDemo) });
            CustomerControlList.Add(new ControlItem() { Name = "消息", NaviName = nameof(MessageDemo) });
            CustomerControlList.Add(new ControlItem() { Name = "消息框", NaviName = nameof(MessageBoxDemo) });
            CustomerControlList.Add(new ControlItem() { Name = "Dialog", NaviName = nameof(DialogDemo) });

            BusinessControlList = new ObservableCollection<ControlItem>();
            BusinessControlList.Add(new ControlItem() { Name = "颜色选择器", NaviName = nameof(XiaFColorPickerDemo) });
            BusinessControlList.Add(new ControlItem() { Name = "仪表盘", NaviName = nameof(DashboardDemo) });
        }

        [RelayCommand]
        private void Navigate(ControlItem controlItem)
        {
            foreach (var item in ControlList)
            {
                if (item.NaviName.Equals(controlItem.NaviName))
                    item.IsChecked = true;
                else
                    item.IsChecked = false;
            }
            foreach (var item in CustomerControlList)
            {
                if (item.NaviName.Equals(controlItem.NaviName))
                    item.IsChecked = true;
                else
                    item.IsChecked = false;
            }
            foreach (var item in BusinessControlList)
            {
                if (item.NaviName.Equals(controlItem.NaviName))
                    item.IsChecked = true;
                else
                    item.IsChecked = false;
            }
            Task.Run(() =>
            {
                if (!keyValuePairs.ContainsKey(controlItem.NaviName))
                {
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    var tmp = assembly.GetTypes();
                    Type T = tmp.FirstOrDefault(s => s.Name == controlItem.NaviName);
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        keyValuePairs.Add(controlItem.NaviName, Activator.CreateInstance(T));
                    });
                }
                Application.Current.Dispatcher.Invoke(() =>
                {
                    Content = keyValuePairs[controlItem.NaviName];
                });
            });

            try
            {
                string name = $"Views/{controlItem.NaviName}.xaml";

                string baseDir = AppDomain.CurrentDomain.BaseDirectory;
                int index = baseDir.IndexOf(PROJECT_NAME);
                string fileName = Path.Combine(baseDir.Substring(0, index + PROJECT_NAME.Length), name);
                TextEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(".xaml");
                TextEditor.Load(fileName);
            }
            catch (Exception) { }
        }
    }
    public class ControlItem : ObservableObject
    {
        public string Name { get; set; }
        public string NaviName { get; set; }

        private bool _isChecked = false;
        public bool IsChecked { get { return _isChecked; } set { _isChecked = value; OnPropertyChanged(); } }
    }
}
